package com.zzz.robot201.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.di.component.DaggerMsgComponent;
import com.zzz.robot201.mvp.contract.MsgContract;
import com.zzz.robot201.mvp.model.api.bean.MsgBean;
import com.zzz.robot201.mvp.model.api.bean.MsgReadBean;
import com.zzz.robot201.mvp.presenter.MsgPresenter;
import com.zzz.robot201.mvp.ui.adapter.MsgAdapter;
import com.zzz.robot201.utils.LogUtils;
import com.zzz.robot201.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/30/2020 22:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MsgActivity extends BaseViewActivity<MsgPresenter> implements MsgContract.View {

    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_tittle)
    TextView tvTittle;
    @BindView(R.id.rcy_msg)
    RecyclerView rcyMsg;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_msg_tittle)
    TextView tvMsgTittle;
    @BindView(R.id.tv_msg_content)
    TextView tvMsgContent;
    private int pageNum = 1;
    private int pageSize = 15;
    private MsgAdapter msgAdapter;
    private List<MsgBean.DataBean.ListBean> msgList = new ArrayList<>();
    private boolean IsLoad = false;
    private int pos = 0;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMsgComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_msg; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle(tvTittle, UIUtils.getString(R.string.title_msg));
        if (mPresenter != null) mPresenter.getMsg(pageNum, pageSize);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rcyMsg.setLayoutManager(linearLayoutManager);
        msgAdapter = new MsgAdapter();
        rcyMsg.setAdapter(msgAdapter);
        msgAdapter.setOnItemClickListener(new MsgAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int momentId, int position) {
                tvMsgTittle.setText(msgList.get(position).getMsgTitle());
                tvMsgContent.setText(msgList.get(position).getMsgContent());
                RecyclerView.LayoutManager manager = rcyMsg.getLayoutManager();
                for (int i = 0; i < manager.getChildCount(); i++) {
                    if (i == position) {
                        View view = manager.getChildAt(i);
                        if (view != null) {
                            view.findViewById(R.id.ll_item_msg).setBackground(getResources().getDrawable(R.drawable.ic_select));
                            view.findViewById(R.id.iv_read).setVisibility(View.GONE);
                            if (msgList.get(position).getReadStatus() == 0) {
                                mPresenter.getMsgRead(momentId);
                            }
                        }
                    } else {
                        View view = manager.getChildAt(i);
                        view.findViewById(R.id.ll_item_msg).setBackground(null);
                    }
                }
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() { //下拉刷新
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                IsLoad = false;
                if (mPresenter != null) mPresenter.getMsg(pageNum, pageSize);
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { //上拉加载更多
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageNum++;
                IsLoad = true;
                if (mPresenter != null) mPresenter.getMsg(pageNum, pageSize);
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
    }

    @Override
    public void showLoading() {
        showBaseLoading();
    }

    @Override
    public void hideLoading() {
        hideBaseLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.btn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                killMyself();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void getMsg(MsgBean code) {
        if (code.getData().getList() .size()>0) {
            msgList.addAll(code.getData().getList());
            tvMsgTittle.setText(msgList.get(0).getMsgTitle());
            tvMsgContent.setText(msgList.get(0).getMsgContent());
            if (msgList.get(0).getReadStatus() == 0) {
                mPresenter.getMsgRead(msgList.get(0).getId());
            }
            if (IsLoad) {
                msgAdapter.setNewData(msgList);
            } else {
                msgAdapter.setNewData(code.getData().getList());
                msgAdapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    public void getMsgRead(MsgReadBean code) {
//        msgAdapter.notifyDataSetChanged();
    }
}
